/*
 * COPYRIGHT. HSBC HOLDINGS PLC 2016. ALL RIGHTS RESERVED.
 * 
 * This software is only to be used for the purpose for which it has been
 * provided. No part of it is to be reproduced, disassembled, transmitted,
 * stored in a retrieval system nor translated in any human or computer
 * language in any way or for any other purposes whatsoever without the
 * written consent of HSBC Holdings plc.
 */
package com.pactera.integral.security.authentication;

import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;
import org.springframework.util.StringUtils;

/**
 * The class AuthenticationUserDetailsService.
 */
public class UserDetailsServiceImpl extends JdbcDaoImpl implements AuthenticationUserDetailsService<Authentication> {

    /**
     * @field roleStandard
     */
    private static final String ROLE_STANDARD = "ROLE_STANDARD";
    
    /**
     * @param token token
     * @return UserDetails
     * @see org.springframework.security.core.userdetails.AuthenticationUserDetailsService
     *      #loadUserDetails(org.springframework.security.core.Authentication)
     */
    @Override
    public UserDetails loadUserDetails(final Authentication token) {
        UserDetails userDetails = null;
        // check the acteur is exists
        String staffId = (String) token.getPrincipal();
        if (StringUtils.hasText(staffId)) {
            // use SDAT's name as user name
            // base userDetails
            userDetails = loadUserByUsername(staffId);
        }
        return userDetails;
    }


    /**
     * @param username username
     * @param authorities authorities
     * @see org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl #addCustomAuthorities(java.lang.String, java.util.List)
     */
    @Override
    protected void addCustomAuthorities(final String username, final List<GrantedAuthority> authorities) {
        authorities.add(new SimpleGrantedAuthority(ROLE_STANDARD));
    }
}
